import logging

from sqlalchemy.ext.asyncio import AsyncSession
from starlette.requests import Request
from starlette.responses import Response
from starlette.templating import Jinja2Templates
from starlette_admin import CustomView

from src.presentation.admin_panel.app.stats import get_chart_data

logger = logging.getLogger(__name__)


class HomeView(CustomView):
    async def render(self, request: Request, templates: Jinja2Templates) -> Response:
        session: AsyncSession = request.state.session
        dates, data, table_rows, rows_sums = await get_chart_data(session)
        return templates.TemplateResponse(
            "home.html",
            {
                "request": request,
                "data": data,
                "dates": dates,
                "table_columns": zip(dates, table_rows),
            },
        )
